﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using NoORM.Core;
using NoORM.Core.Contract;
using NoORM.Core.ServiceScript;

namespace Restsql.Api.Controllers
{
    /// <summary>
    /// 从服务端获取表达式
    /// </summary>
    [Route("[controller]/{expression}")]
    [ApiController]
    public class ExpressionController : ControllerBase
    {
        /// <summary>
        /// 表达式服务
        /// </summary>
        private readonly ServiceExpression serviceExpression;

        public ExpressionController(ServiceExpression serviceExpression)
        {
            this.serviceExpression = serviceExpression;
        }

        /// <summary>
        /// 从服务端获取表达式
        /// </summary>
        /// <param name="expression">C#表达式</param>
        /// <returns></returns>
        public async Task<JsonResult> Get(string expression)
        {
            ApiData<object> result = new ApiData<object>();
            result.ServerReceiveTime = DateTime.Now;
            try
            {
                result.Data = await this.serviceExpression.GetExpressionValue(expression);
                result.Success = true;
            }catch(Exception ex)
            {
                result.Message = ex.Message;
                result.Success = false;
            }
            result.ServerSendTime = DateTime.Now;
            return new JsonResult(result);
        }
    }
}
